package com.jpyl.jpMedicalTreatment.http;

import android.content.Context;
import android.util.Log;

import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LMX on 2017/1/10.
 */
public class HttpClient implements IHttpClient{

    public final static String TAG = "HttpClient";

    private static HttpClient client;
    private Context context;

    public static HttpClient getInstance(Context context){
        if(client == null){
            client = new HttpClient();
        }
        client.context = context;
        return client;
    }

    @Override
    public void get(String url, final Callback callback) {
        Log.e(TAG, "get:begin->>>>>"+url);
        Ion.with(context)
                .load(url)
                .asString()
                .setCallback(new FutureCallback<String>() {
                    @Override
                    public void onCompleted(Exception e, String result) {
                        if(callback != null){
                            callback.onFinal();
                        }
                        if(e == null){
                            Log.e(TAG, "get:result->>>>>"+result);
                            if(callback != null){
                                callback.onSucceed(result);
                            }
                        }else{
                            Log.e(TAG, "get:error->>>>>"+e);
                            if(callback != null){
                                callback.onFailed(e);
                            }
                        }
                    }
                });

    }

    @Override
    public void post(final String url, final Map<String, String> params, final File file, final Callback callback) {
        Log.e(TAG, "post:begin->>>>>"+url);
        Log.e(TAG, "post:params->>>>>"+params);
        Log.e(TAG, "post:file->>>>>"+file);
        Map<String, List<String>> map = new HashMap<>();
        if(params != null) {
            for (String key : params.keySet()) {
                List<String> list = new ArrayList<String>();
                list.add(params.get(key));
                map.put(key, list);
            }
        }
        Ion.with(context)
                .load(url)
                .setMultipartFile("file", file)
                .setMultipartParameters(map)
                .asString()
                .setCallback(new FutureCallback<String>() {
                    @Override
                    public void onCompleted(Exception e, String result) {
                        if(callback != null){
                            callback.onFinal();
                        }
                        if(e == null){
                            Log.e(TAG, "post:result->>>>>"+result);
                            if(callback != null){
                                callback.onSucceed(result);
                            }
                        }else{
                            Log.e(TAG, "post:error->>>>>"+e);
                            if(callback != null){
                                callback.onFailed(e);
                            }
                        }
                    }
                });
    }

    @Override
    public void post(final String url, final JsonObject body, final Callback callback) {
        Log.e(TAG, "post:begin->>>>>"+url);
        Log.e(TAG, "post:body->>>>>"+body);
        Ion.with(context)
                .load(url)
                .setJsonObjectBody(body)
                .asString()
                .setCallback(new FutureCallback<String>() {
                    @Override
                    public void onCompleted(Exception e, String result) {
                        if(callback != null){
                            callback.onFinal();
                        }
                        if(e == null){
                            Log.e(TAG, "post:result->>>>>"+result);
                            if(callback != null){
                                callback.onSucceed(result);
                            }
                        }else{
                            Log.e(TAG, "post:error->>>>>"+e);
                            if(callback != null){
                                callback.onFailed(e);
                            }
                        }
                    }
                });
    }

}
